package com.qfedu.yuxiaoer.service.impl;

import com.github.pagehelper.PageHelper;
import com.qfedu.yuxiaoer.common.api.CommonResult;
import com.qfedu.yuxiaoer.entity.HouseLease;
import com.qfedu.yuxiaoer.mapper.HouseLeaseMapper;
import com.qfedu.yuxiaoer.service.HouseLeaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description TODO
 * @Author Dc
 * @Date 2023/2/15 21:04
 */
@Service
public class HouseLeaseServiceImpl implements HouseLeaseService {

    @Autowired
    private HouseLeaseMapper houseLeaseMapper;

    @Override
    public CommonResult addHouseLease(HouseLease houseLease) {
        if (!houseLease.getCommunity().isEmpty()) {
            if (!houseLease.getFloor().isEmpty()) {
                if (!houseLease.getRoom().isEmpty()) {
                    if (houseLeaseMapper.insert(houseLease) > 0) {
                        return CommonResult.success("新增整租/合租成功");
                    } else {
                        return CommonResult.failed("新增整租/合租失败");
                    }
                } else {
                    return CommonResult.failed("房间号不能为空");
                }
            } else {
                return CommonResult.failed("楼层不能为空");
            }
        } else {
            return CommonResult.failed("小区不能为空!");
        }
    }

    @Override
    public CommonResult<HouseLease> findHouseLeaseById(Integer id) {
        if (id > 0) {
            if (houseLeaseMapper.selectByPrimaryKey(id) != null) {
                return CommonResult.success(houseLeaseMapper.selectByPrimaryKey(id), "查询成功!");
            } else {
                return CommonResult.failed("查询失败");
            }
        } else {
            return CommonResult.failed("ID不能为空或者负数");
        }
    }

    @Override
    public CommonResult<List<HouseLease>> findHouseLeaseByCommunity(int page, int limit, String community) {
        PageHelper.startPage(page, limit);
        List<HouseLease> houseLeases = houseLeaseMapper.selectByCommunity(community);
        return CommonResult.success(houseLeases);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id > 0) {
            if (houseLeaseMapper.deleteByPrimaryKey(id) > 0) {
                return CommonResult.success("删除成功");
            } else {
                return CommonResult.failed("删除失败");
            }
        } else {
            return CommonResult.failed("ID不能为空或者负数");
        }
    }
}
